//
//  ProfileHomeViewController.swift
//  App_Profile
//
//  Created by 蒋晨成 on 2021/12/27.
//

import Adaptor
import App_Common_Views
import Center
import Chain
import Foundation
import Kingfisher
import PullToRefreshKit
import RxCocoa
import RxSwift
import Service
import SnapKit
import Toast
import UICore
import UIKit

class ProfileHomeViewController: ViewController {
    private let disposeBag = DisposeBag()

    private lazy var viewModel = ProfileHomeViewModel(disposeBag: disposeBag)

    lazy var refreshHeader: SimpleRefreshHeader = RefreshView.header()
    let refreshData = PublishSubject<Void>()
    private lazy var contentView: ProfileHomeContentView = {
        let contentView = ProfileHomeContentView()
        contentView.infoView.delegate = self
        contentView.backgroundColor = Adaptor.Profile.background
        contentView.configRefreshHeader(with: refreshHeader, container: self) { [weak self] in
            self?.refreshData.onNext(())
        }
        return contentView
    }()
}

// MARK: - Override

extension ProfileHomeViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        setupNav()
        setup()
        bindData()
    }
}

private extension ProfileHomeViewController {
    func setupNav() {
        if (navigationController?.viewControllers.count ?? 0) > 1 {
            let backImage = ProfileModular.image(named: "Common.Nav.Action.Back")
            addDefaultLeftBarButtonItem(image: backImage)
        }

        let barButtonItem = UIBarButtonItem(
            image: ProfileModular.image(named: "Profile.Nav.Action.Setting"),
            style: .plain,
            target: self,
            action: #selector(settingClicked)
        )
        navigationItem.rightBarButtonItem = barButtonItem

        setNavigationBarTintColor(tintColor: Adaptor.Profile.titleColor)
    }

    func setup() {
        view.backgroundColor = Adaptor.Profile.background
        contentView.x.add(to: view).snp.makeConstraints {
            $0.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            $0.trailing.leading.bottom.equalToSuperview()
        }
    }
}

private extension ProfileHomeViewController {
    func bindData() {
        let input = ProfileHomeViewModel.Input(refreshData: refreshData)
        let output = viewModel.transform(input: input)
        output.name
            .asDriver(onErrorJustReturn: "")
            .distinctUntilChanged()
            .drive(contentView.infoView.userNameLabel.rx.text)
            .disposed(by: disposeBag)

        output.signature
            .asDriver(onErrorJustReturn: "")
            .distinctUntilChanged()
            .drive(contentView.infoView.signatureLabel.rx.text)
            .disposed(by: disposeBag)

        output.avatar
            .asDriver(onErrorJustReturn: "")
            .distinctUntilChanged()
            .drive(onNext: { [weak self] string in
                self?.contentView.infoView.avatarView.set(url: URL(string: string), placeholder: ProfileModular.image(named: "Common.Avatar.Placeholder"))
            })
            .disposed(by: disposeBag)

        output.refresh.asDriver(onErrorJustReturn: false)
            .drive { [weak self] _ in
                self?.contentView.switchRefreshHeader(to: .normal(.success, 0.2))
            }
            .disposed(by: disposeBag)

        viewModel.resetData()
    }
}

private extension ProfileHomeViewController {
    @objc
    func settingClicked() {
        Router.push(pattern: "profile/setting")
    }
}

extension ProfileHomeViewController: ProfilePersonalInfoViewDelegate {
    func infoViewDidSelected(_: ProfilePersonalInfoView) {
        if UserCenter.isLogin {
            Router.push(pattern: "profile/edit")
        } else {
            Router.push(pattern: "login")
        }
    }
}

// MARK: - Routable

extension ProfileHomeViewController: Routable {
    static func initialize(values _: [String: Any]) -> ViewController? {
        ProfileHomeViewController(nibName: nil, bundle: nil)
    }

    static var routePattern = "profile/home"

    static let description: String = "Profile Home Router"
}
